'''
#Coding: utf-8
#Project: online-exam
#Author: rtf
#Time: 2022-08-02 18:39:10
#FileName: student_score.py
#Softwar: PyCharm
'''


import json
import random
from trump.query import get_item, get_items, get_table_header, modify_item
from trump.utils import ok, fail
from trump.decorators import table_headers
from utils.params_validate_decorator import validate_params
from funcs.user_info import get_uesr_info
from trump.utils import get_user


async def ls(app, request):
    params = request.args
    user = get_user(request)
    role = user.get("role")
    if role == "STUDENT":
        params["student_no"] = (user.get("student_no"),)


async def post_ls(app, request, data):
    exam_paper = await get_items(app.ctx.pool, "exam_paper")
    course = await get_items(app.ctx.pool, "course")

    for i in data:
        for q in exam_paper:
            if i.get("exam_paper_id") == q.get("id"):
                i["exam_paper_name"] = q.get("name")
                for x in course:
                    if q.get("course_code") == x.get("code"):
                        i["course_name"] = x.get("name")


async def get(app, request, id):
    pass


async def post_get(app, request, data, id):
    exam_paper_id = data.get("exam_paper_id")
    exam_paper = await get_item(app.ctx.pool, "exam_paper", exam_paper_id)
    if exam_paper:
        exam_questions = json.loads(exam_paper.get("exam_questions"))
        nums = 0
        for x in exam_questions:
            nums += len(x.get("questions"))
            for s in x.get("questions"):
                s["answer"] = "-"

        data["nums"] = [x for x in range(1, nums+1)]
        data["questions"] = exam_questions
